<template>
  <div class="login-container">
    <div class="login-left" />
    <div class="login-right">
      <div class="form">
        <h2>登录</h2>
        <div class="login">
          <el-form ref="formdataRlues" :model="formdata" :rules="formdataRules">
            <el-form-item prop="mobile">
              <el-input v-model="formdata.mobile" />
            </el-form-item>
            <el-form-item prop="password">
              <el-input v-model="formdata.password" show-password />
            </el-form-item>
            <el-form-item prop="isAgreen">
              <el-checkbox v-model="formdata.isAgreen">用户平台使用协议</el-checkbox>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="doLogin">登录</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>

</template>

<script>
// import { sysLogin } from '@/api/user'
export default {
  data() {
    return {
      formdata: {
        mobile: '13800000002',
        password: '123456',
        isAgreen: false
      },
      formdataRules: {
        mobile: [
          { required: true, message: '账号不能为空', trigger: 'blur' },
          { pattern: /^1[3|5|6|7|8|9]\d{9}$/, message: '手机号格式不对', trigger: 'blur' }],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { pattern: /^\w{6,20}$/, message: '密码在6-20位', trigger: 'blur' }
        ],
        // 自定义的校验
        isAgreen: [{ validator: (rule, value, callback) => {
          value ? callback() : callback(new Error('请勾选协议'))
        }, trigger: 'change' }]
      }
    }
  },
  methods: {
    async doLogin() {
      await this.$refs.formdataRlues.validate()
      console.log('校验成功！')
      // 如果没有catch，浏览器会报错，利用try包裹，防止浏览器报红
      try {
        this.$store.dispatch('user/loginAction', this.formdata)
      } catch (err) {
        //
      }
    }
  }
}
</script>

<style lang="scss" scoped>
 .login-container{
//  background-color: red;
  height: 100vh;
  display: flex;
  .login-left{
    flex: 3;
    //添加~
    background-image: url('~@/assets/common/login_back.png');
    background-size: cover;
  }
  .login-right{
    flex: 2;
    display: flex;
    justify-content: center;
    align-items: center;
    .login{
      width: 350px;
      height: 200px;
      .el-button{
        width: 350px;
      }
      .el-input__inner{
        background-color: #666;
      }
    }
  }
}
</style>
